package kaka.utils.effect
{
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Back;
	import com.greensock.plugins.MotionBlurPlugin;
	import com.greensock.plugins.TweenPlugin;
	
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	import mx.core.UIComponent;
	import mx.effects.Blur;
	import mx.effects.Fade;

	/**
	 * Flash图形分布
	 * @author kaka
	 * @date 2011.9
	 **/
	public class DistributeUtils
	{
		/**
		 * 动画执行方向参数
		 **/
		public static const UP_TO_DOWN:String="up_to_down";
		public static const DOWN_TO_UP:String="down_to_up";
		public static const LEFT_TO_RIGHT:String="left_to_right";
		public static const RIGHT_TO_LEFT:String="right_to_left";
		
		public function DistributeUtils()
		{
		}
		/**
		 * 圆形分布
		 * @param objs        需要进行圆形分布的显示对象集合
		 * @param centerPoint 中心点坐标
		 **/
		public static function circularDistribution(objs:Array,centerPoint:Point):void
		{
			//显示对象总数
			var totalNum:int=objs.length;
			
			//每个对象的角度
			var perAngle:Number=2*Math.PI/totalNum;
			
			for (var i:int=0; i<totalNum; i++)  
			{  
				trace(i);
				//坐标分布 
				var toX:Number=250+Math.cos(i*perAngle)*130; 
				var toY:Number=200+Math.sin(i*perAngle)*130; 
				TweenLite.from(objs[i],0.5,{x:centerPoint.x,y:centerPoint.y,alpha:0.0});
				TweenLite.to(objs[i],1.0,{x:toX,y:toY,alpha:1.0,ease:Back.easeOut});
			}  
		}
		/**
		 * 花洒式分布
		 * @param childList 需要被分布的显示对象列表
		 * @param container 装载容器
		 **/
		public static function awisEffect(childList:ArrayCollection,container:UIComponent,rowGap:int,direction:String=UP_TO_DOWN):void
		{ 
			var itemWidth:Number=childList.getItemAt(0).width;
			var itemHeight:Number=childList.getItemAt(0).height;
			/**每行Item个数**/
			var itemNum:int=Math.floor(container.width/itemWidth);
			trace("itemNum:"+itemNum);
			/**每个Item的间隙**/
			var itemGap:int=(container.width/itemWidth)/itemNum;
			//var rowGap:int=(container.height/itemHeight)/itemNum+5;
			trace("itemGap:"+itemGap);
			/**初始位置**/
			var startX:Number=0;
			var startY:Number=0;
			switch(direction)
			{
				case UP_TO_DOWN:
					startX=container.width/2;
					startY=-container.height/2;
					break;
				case DOWN_TO_UP:
					startX=container.width/2;
					startY=container.height*1.5;
					break;
				case LEFT_TO_RIGHT:
					startX=-container.width/2;
					startY=container.height/2;
					break;
				case RIGHT_TO_LEFT:
					startX=container.width*1.5;
					startY=container.height/2;
					break;
			}
			//显示对象轴初始位置
			var endX:Number=itemGap;
			var endY:Number=rowGap;
			for(var i:int=0;i<childList.length;i++)
			{
				endX=((i%itemNum)+1)*itemGap+(i%itemNum)*childList.getItemAt(i).width;
				trace(i%itemNum);
				//换行
				if(i%itemNum==0)
				{
					endY=(i+1)*rowGap+(i%itemNum)*childList.getItemAt(i).height;
				}
				TweenLite.from(childList.getItemAt(i),0.5,{x:startX,y:startY,alpha:0.0});
				TweenLite.to(childList.getItemAt(i),1.0,{x:endX,y:endY,alpha:1.0,delay:i*0.1,ease:Back.easeOut});
				
			}
			
		}
		private static function excuteAwisEffectMove(childList:ArrayCollection,container:UIComponent,rowGap:int=20):void
		{
			/**每个Item间隙**/
			var itemGap:Number=container.width/(childList.length+1);
			/**每行显示个数**/
			var columnNum:int=container.width/(itemGap+childList[0].width);
			
			var endX:Number=itemGap;
			var endY:Number=rowGap;
			for(var i:int=0;i<childList.length;i++)
			{
				endX=((i%columnNum)+1)*itemGap+(i%columnNum)*childList.getItemAt(i).width;
				trace(i%columnNum);
				//换行
				if(i%columnNum==0)
				{
					endY=(i+1)*rowGap+(i%columnNum)*childList.getItemAt(i).height;
				}
				TweenLite.to(childList.getItemAt(i),1.0,{x:endX,y:endY,alpha:1.0,delay:i*0.1,ease:Back.easeOut});
			}
		}
		/**
		 * 块状分布
		 * @param sources 		      块状对象集合 
		 * @param excuteTime       执行动画时间(单位：默认1.0秒) for example:1.0
		 * @param direction        分布方向 UP_TO_DOWN(默认)
		 * @param columnCount 	      纵向块数
		 * @param rowCount	              横向块数
		 * @param angle			      旋转角度
		 * @param onComplete       所有分布运动完成时执行
		 * @param onCompleteParams onComplete参数
		 **/
		public static function blockDistribute(sources:Array,
											   excuteTime:Number=1.5,
											   direction:String=UP_TO_DOWN,
											   columnCount:int=0,
											   rowCount:int=0,
											   angleX:int=180,
											   angleY:int=0,
											   angleZ:int=0,
											   initAlpha:Number=0.0,
											   onComplete:Function=null,
											   onCompleteParams:Array=null):void
		{
			var length:int=sources.length;
			var _toX:Number=0;
			var _toY:Number=0;
			for(var i:int=0;i<length;i++)
			{
				switch(direction)
				{
					case UP_TO_DOWN:
						_toX=sources[i].x;
						_toY=columnCount*sources[i].height;
						break;
					case DOWN_TO_UP:
						_toX=sources[i].x;
						_toY=columnCount*sources[i].height+sources[i].y;
						break;
					case LEFT_TO_RIGHT:
						_toX=sources[i].x; 
						_toY=sources[i].y;
						break;
					case RIGHT_TO_LEFT:
						_toX=rowCount*sources[i].width;
						_toY=sources[i].y;
				}
				if(i==length-1)
				{
					TweenLite.from(sources[i],excuteTime,{delay:0.1*i,
														  x:_toX,
														  y:_toY,
														  rotationX:angleX,
														  rotationY:angleY,
														  rotationZ:angleZ,
														  alpha:0.0,
														  ease:Back.easeOut,
														  onComplete:onComplete,
														  onCompleteParams:onCompleteParams});
				}
				else
				{
					TweenLite.from(sources[i],excuteTime,{delay:0.1*i,
														  x:_toX,
														  y:_toY,
														  rotationX:angleX,
														  rotationY:angleY,
														  rotationZ:angleZ,
														  alpha:0.0,
														  ease:Back.easeOut});
				}
			}
		}
		
	}
}